package com.xxxx.portal.controller;


import com.xxxx.common.result.RespBean;
import com.xxxx.portal.service.UserService;
import com.xxxx.portal.vo.Captcha;
import com.xxxx.portal.vo.UserName;
import com.xxxx.portal.vo.UserVo;
import com.xxxx.rpc.service.SendMessageService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;

//用户注册实现
@Controller
public class RegisterController {

    @Autowired
    private UserService userService;
    //注入发送短信接口
    @Reference(interfaceClass = SendMessageService.class)
    private SendMessageService sendMessageService;

    @RequestMapping("/{page}")
    public String page(@PathVariable String page) {
        System.out.println(page);
        return page;
    }

    //用户注册功能，成功并发送注册短信
    @PostMapping("/user/register")
    @ResponseBody
    public RespBean registerUser(@Valid UserVo userVo){
        RespBean respBean = userService.registerUser(userVo);
        //返回的结果集ok，则发送短信
        if(200==respBean.getCode()){
            //发送短信ok 则返回成功提示信息
            RespBean respBean1 = sendMessageService.SendMessage(userVo.getEmail());
            if(200==respBean1.getCode()){
                System.out.println("注册短信已发送！");
                return RespBean.success("注册短信发送成功！");
            }
        }
        return RespBean.error("error");
    }

    //注册时生成验证码
    @PostMapping("/getCaptcha")
    @ResponseBody
    public RespBean getCaptcha(HttpServletRequest request, Captcha captcha){
        String ticket = request.getParameter("ticket");
        String randStr = request.getParameter("randStr");
        if (ticket.equals(captcha.getTicket()) && randStr.equals(captcha.getRandStr())) return RespBean.success("操作成功");
        return RespBean.error("操作失败");

    }



}
